package com.yunhuajinfu.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yunhuajinfu.tool.SharedPreferencesUtils;
import com.yunhuajinfu.view.TitleView;
import com.yunhuajinfu.view.statusView.StatusView;
import com.yunhuajinfu.view.swipetoloadlayout.base.SwipeToLoadLayout;


/**
 * 作者：黑哥 on 2016/3/29 09:54
 */
public abstract class BaseFragment extends Fragment implements  View.OnClickListener {
    //title
    protected TitleView titleView;
    protected String TAG=this.getClass().getSimpleName();
    protected BaseActivity activity;
    //调用下面方法显示布局
    private StatusView status;
    protected   View rootView;

//    protected LoadingDialog loadingDialog;
    //
   protected  SharedPreferencesUtils mpreferencesUtils;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity= (BaseActivity) getActivity();
         mpreferencesUtils = SharedPreferencesUtils.getInstance();
//        loadingDialog=new LoadingDialog(activity,R.style.MyDialogStyle);


    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater,container,savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        this.rootView=view;
        //getChildFragmentManager().beginTransaction().add(view.getId(),new ProjectFragment());
        initTitle();
        initView();
        setData();



    }
    //记录原来view的状态
    private int paddingLeft,paddingTop,paddingRight,paddingBottom;
    protected void initStatus(){
        if (status==null){
            status=new StatusView(activity);
            status.hintLineView();
        }
        if (status.getParent()==null){
            final ViewGroup group= (ViewGroup) rootView;
            //保存参数状态
            paddingLeft=group.getPaddingLeft();
            paddingTop=group.getPaddingTop();
            paddingRight=group.getPaddingRight();
            paddingBottom=group.getPaddingBottom();

            group.setPadding(0, 0, 0, 0);
            group.post(new Runnable() {
                @Override
                public void run() {

                    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(group.getWidth(), group.getHeight());
                    for (int i = 0; i < group.getChildCount(); i++) {
                        group.getChildAt(i).setVisibility(View.GONE);
                    }
                    if (status.getParent()!=null){
                        status.removeAttachView();
                    }
                    group.addView(status, layoutParams);
                }
            });

        }

    }

    /**
     * 清除状态布局
     */
    protected void removeStatus(){
        if (status==null){
            return;
        }
        status.removeAttachView();
        ViewGroup group= (ViewGroup) rootView;
        //rootView.setLayoutParams(oldParams);
        for (int i=0;i<group.getChildCount();i++){
            group.getChildAt(i).setVisibility(View.VISIBLE);
        }
        group.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
    }
    /**
     * 全屏显示无数据状态,无点击事件
     */
    protected void showNoData(String message){
        initStatus();
        status.NoData(message);
    }
    /**
     * 全屏显示无数据状态,有点击事件
     */
    protected void showNoData( String message,  String btnText, View.OnClickListener listener){
        initStatus();
        status.NoData(message,btnText,listener);
    }

    /**
     * 全屏显示无网络状态,有点击事件
     * @param listener
     */
    protected void showNoNewWork(View.OnClickListener listener){
        initStatus();
        status.NoNewWork(listener);
    }

    /**
     * 全屏显示加载中状态
     */
    protected void showLoading(){
        initStatus();
        status.Loading();
    }
    protected abstract void initTitle();
    protected abstract void initView();
    protected abstract void setData();
    //事件监听
    protected abstract void widgetClick(View view);

    /**
     * Created by yinsujun on 2016/4/1 0001 13:33
     * 设置标题及返回键
     * title:标题名称
     * id：title
     */
    protected void setTitle(String title) {
//        titleView = (TitleView) rootView.findViewById(R.id.title);
//        titleView.setTitle(title);
    }

    protected void onComplete(SwipeToLoadLayout refresh){
        if (refresh!=null){
            if (refresh.isRefreshing()){
                refresh.setRefreshing(false);
            }else if (refresh.isLoadingMore()){
                refresh.setLoadingMore(false);
            }
        }

    }
    protected void autoLoading(final SwipeToLoadLayout refresh,RecyclerView swipeTarget){
        //自动上拉
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        refresh.setLoadingMore(true);
                    }
                }
            }
        });
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        startActivity(intent);
    }

    public void startActivity(Bundle bundle, Class<?> cls) {
        Intent intent = new Intent(activity, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(fastClick()){
            widgetClick(v);
        }
    }

    /**
     *
     * @return 控制短时间内重复点击事件－－－－看个人爱好而定
     */
    private long lastClick =0;
    private boolean fastClick(){
        if(System.currentTimeMillis()-lastClick<=500){
            return false;
        }

        lastClick=System.currentTimeMillis();
        return true;

    }

}
